package com.hxut.mapper;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxut.domain.Book;

@SuppressWarnings("all")
@SpringBootTest
public class BookTestCase {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private Book book;

    /**
     * 测试根据id进行查询
     */
    @Test
    void testGetById(){
        System.out.println(bookMapper.selectById(1));
    }

    /**
     * 测试增加
     */
    @Test
    void testSave(){
        book.setType("测试数据123");
        book.setName("测试数据123");
        book.setDescription("测试数据123");
        bookMapper.insert(book);
    }

    /**
     * 测试更新
     */
    @Test
    void testUpdate(){
        book.setId(19);
        book.setType("测试数据123");
        book.setName("测试数据123sds");
        book.setDescription("测试数据123");
        bookMapper.updateById(book);
    }

    /**
     * 测试删除
     */
    @Test
    void testDelete(){
        System.out.println();
    }

    /**
     * 测试查询全部
     */
    @Test
    void testGetAll(){
        System.out.println(bookMapper.selectList(null));
    }

    /**
     * 测试分页
     */
    @Test
    void testGetPage(){
        IPage page=new Page(1,5);
        bookMapper.selectPage(page,null);
        System.out.println(page.getCurrent());//当前的页码
        System.out.println(page.getSize());//每页多少数据
        System.out.println(page.getTotal());//总记录条数
        System.out.println(page.getRecords());//数据
    }

    /**
     * 测试条件查询
     */
    @Test
    void testGetBy(){
        //QueryWrapper就相当于条件查询
        QueryWrapper<Book>  qw = new QueryWrapper<>();
        //相当于select * from tbl_book where name like %Java%
        qw.like("name","Java");
        bookMapper.selectList(qw);
    }

    @Test
    void testGetBy2(){
        String name="1";
        LambdaQueryWrapper<Book>  lqw = new LambdaQueryWrapper<>();
//        if(name!=null)  lqw.like(Book::getName,name);
        lqw.like(name!=null,Book::getName,name);
        bookMapper.selectList(lqw);
    }

}
